<script setup>
import { ref, onMounted } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import { ElMessage, ElMessageBox } from 'element-plus';
import { getRoleListAPI, getTreeListAPI, getRoleDetailAPI, getRoleUserListAPI, delRoleUserAPI } from '@/api/system';
import { User, More } from '@element-plus/icons-vue';

const roleList = ref([]);
const activeIndex = ref(0);
const treeList = ref([]);
const defaultProps = ref({ label: 'title', children: 'children'});
const perms = ref([]); // 当前角色权限点列表
const activeName = ref('permission'); // 默认打开permission
const params = ref({
  page: 1,
  pageSize: 10,
});
const roleUserList = ref([]); // 当前角色下的成员列表
const total = ref(0); // 当前角色下的成员总数
const treeRefs = ref(null); // 获取功能当前的节点信息
const route = useRoute(); // 获取当前路由实例
const router = useRouter(); // 实时控制路由跳转

let roleId = route.query.id;

onMounted(async () => {
  await getRoleList();
  await getTreeList();
  getRoleDetail(roleList.value[0].roleId);
  getRoleUserList(roleList.value[0].roleId, params.value);
});
// 切换部门
function changeRole(index) {
  activeIndex.value = index;
  const roleId = roleList.value[index].roleId;
  getRoleDetail(roleId);
  getRoleUserList(roleId, params.value);
}
// 获取部门数据信息
async function getRoleList() {
  const res = await getRoleListAPI();
  roleList.value = res.data;
}
// 获取所有功能权限节点信息
async function getTreeList() {
  const res = await getTreeListAPI();
  treeList.value = res.data;
  console.log('treeList',treeList.value);
  console.log('treeRefs',treeRefs.value);
  
  addDisabledProps(treeList.value);
}
// 让每个节点都显示为禁用状态
function addDisabledProps(treeList) {
  treeList.forEach(item => {
    item.disabled = true;
    if (item.children) {
      addDisabledProps(item.children);
    }
  });
}
// 获取部门数据，以及他的功能节点

async function getRoleDetail(roleId) {
  console.log(roleId);
  
  const res = await getRoleDetailAPI(roleId);

  perms.value = res.data.perms;
  console.log(treeRefs.value);
  
  if (treeRefs.value) {
    treeRefs.value.forEach((item, index) => {
      item.setCheckedKeys(perms.value[index]);
    });
  }
}
// 获取部门中的用户数据
async function getRoleUserList(roleId) {
  const res = await getRoleUserListAPI(roleId, params.value);
  roleUserList.value = res.data.rows;
  total.value = res.data.total;
}
// 删除功能
async function delRole(roleId) {

  ElMessageBox.confirm(
    '确定要删除当前角色吗?',
    '温馨提示',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then( async () => {
      await delRoleUserAPI(roleId);
      getRoleList();
      ElMessage({
        type: 'success',
        message: '删除成功',
      })
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '操作失败',
      })
    })
}
</script>

<template>
  <div class="role-container">
    <!-- 左侧部门导航栏 -->
    <div class="left-wrapper">
      <div
        v-for="(item, index) in roleList"
        :key="item.roleId"
        class="role-item"
        :class="{ active: index === activeIndex }"
        @click="changeRole(index)"
      >
        <div class="role-info">
          <el-icon :name="index === activeIndex ? 'user-active' : 'user'" class="icon" ><User /></el-icon>
          {{ item.roleName }}
        </div>
        <div class="more">
          <el-dropdown>
            <span class="el-dropdown-link">
              <el-icon class="more"><More /> </el-icon>
            </span>
            <template #dropdown>
              <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click="() => router.push(`/role/addPart?id=${item.roleId}`)">
                编辑角色
              </el-dropdown-item>
              <el-dropdown-item @click="() => delRole(item.roleId)">
                删除
              </el-dropdown-item>
            </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </div>
      <el-button class="addBtn" type="primary"  @click="() => router.push('/role/addPart')">添加角色</el-button>
    </div>
    <!-- 右部功能显栏 -->
    <div class="right-wrapper" style="margin-left: 15px;">
      <el-tabs v-model="activeName">
        <!-- 功能权限栏 -->
        <el-tab-pane  label="功能权限" name="permission">
          <div class="tree-wrapper">
            <div v-for="item in treeList" :key="item.id" class="tree-item">
              <div class="tree-title">{{ item.title }}</div>
              <el-tree
                ref="treeRefs"
                :data="item.children"
                :props="defaultProps"
                show-checkbox
                default-expand-all
                node-key="id"
              />
            </div>
          </div>
        </el-tab-pane>
        <!-- 成员列表栏 -->
        <el-tab-pane :label="`成员列表(${total})`" name="member">
          <div class="user-wrapper">
            <el-table :data="roleUserList" style="width: 100%">
              <el-table-column type="index" width="250" label="序号" />
              <el-table-column prop="name" label="员工姓名" />
              <el-table-column prop="userName" label="登录账号" />
            </el-table>
          </div>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>




<style scoped lang="scss">
.role-container {
  display: flex;
  font-size: 14px;
  background-color: #fff;
  padding:20px;
  .left-wrapper {
    min-width: 200px;
    border-right: 1px solid #e4e7ec;
    padding: 4px;
    text-align: center;

    .role-item {
      position: relative;
      height: 56px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 20px;
      cursor: pointer;

      &.active {
        background: #edf5ff;
        color: #4770ff;
      }
    }

    .role-info {
      display: flex;
      align-items: center;

      .icon {
        margin-right: 10px;
        font-size: 18px;
      }
    }

    .more {
      display: flex;
      align-items: center;
    }
    .addBtn{
      width: 100%;
      margin-top: 20px;
    }
  }

  .right-wrapper {
    flex: 1;

    .tree-wrapper {
      display: flex;
      justify-content: space-between;

      .tree-item {
        flex: 1;
        border-right: 1px solid #e4e7ec;
        padding: 0px 4px;
        text-align: center;
        .tree-title {
          background: #f5f7fa;
          text-align: center;
          padding: 10px 0;
          margin-bottom: 12px;
        }
      }
    }

    // ::v-deep .el-tabs__nav-wrap {
    //   padding-left: 20px;
    // }

    .user-wrapper{
      padding:20px;
    }
  }
}
</style>